#ifndef SERVERTCP_H
#define SERVERTCP_H

#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
#include <QMutex>

// Server 类继承自 QTcpServer，作为服务器的核心类
class ServerTcp : public QTcpServer {
    Q_OBJECT

public:
    explicit ServerTcp(QObject *parent = nullptr);
    ~ServerTcp(); // 添加析构函数声明
    // 析构函数
    bool startServer(quint16 port);

    void onAssignClientRequests(QTcpSocket *m_pTcpSocket);    //分配客户端请求

signals:
    void onServerLogin(const QString &tempUsername, const QString &tempPassword, QTcpSocket *socket);        // 服务器登录验证信号
    void onServerGetDevive(QTcpSocket *socket);

public slots:
    void onRespondClientLogin(bool isActive, QTcpSocket* socket);     //回复客户端登录请求
    void onRespondClientDevice(QString Device, QTcpSocket *socket);    //回复客户端设备信息请求

protected:
    void incomingConnection(qintptr socketDescriptor) override;          //处理新的客户端连接


private:
    QList<QTcpSocket*> m_clients;
    QMutex m_clientsMutex;
    QMap<QTcpSocket*, QString> m_buffers; // 成员变量
    QMutex m_bufferMutex;
    static QMap<QTcpSocket*, QByteArray> m_pendingSockets;
    static QMutex m_pendingMutex;
};
#endif // SERVERTCP_H
